package com.hzo.great.leisuregreat.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hzo.great.leisuregreat.R;
import com.hzo.great.leisuregreat.beans.PhoneListDataBean;

import java.util.List;

public class PhoneAllListAdapter extends RecyclerView.Adapter {
    private List<PhoneListDataBean> list;//存放数据
    private Context mContext;

    public PhoneAllListAdapter(List<PhoneListDataBean> list, Context context) {
        this.list = list;
        this.mContext = context;
    }

    private final static int HEAD_COUNT = 1;
    private final static int FOOT_COUNT = 0;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;
    private final static int TYPE_FOOTER = 2;

    private int getContentSize(){
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        int contentSize = getContentSize();
        if (HEAD_COUNT != 0 && position == 0){ // 头部
            return TYPE_HEAD;
        }else if(FOOT_COUNT != 0 && position == HEAD_COUNT + contentSize){ // 尾部
            return TYPE_FOOTER;
        }else{
            return TYPE_CONTENT;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.layout_item_head, parent, false);
            return new HeadHolder(itemView);
        }
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.home_list_item, parent, false));
    }

    //在这里可以获得每个子项里面的控件的实例，比如这里的TextView,子项本身的实例是itemView，
    // 在这里对获取对象进行操作
    //holder.itemView是子项视图的实例，holder.textView是子项内控件的实例
    //position是点击位置
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof PhoneAllListAdapter.MyViewHolder){ // 内容
            PhoneAllListAdapter.MyViewHolder holderBody = (PhoneAllListAdapter.MyViewHolder) holder;
            holderBody.txt_home_phone_name.setText(list.get(position - 1).getKey());
            holderBody.recycler_home_item.setLayoutManager(new GridLayoutManager(mContext, 2));
            holderBody.recycler_home_item.setAdapter(new PhoneListAdapter(list.get(position - 1).getPhoneList(),mContext));
        }
    }

    //要显示的子项数量
    @Override
    public int getItemCount() {
        return list.size() + HEAD_COUNT + FOOT_COUNT;
    }

    //这里定义的是子项的类，不要在这里直接对获取对象进行操作
    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView txt_home_phone_name;
        RecyclerView recycler_home_item;

        MyViewHolder(View itemView) {
            super(itemView);
            txt_home_phone_name = itemView.findViewById(R.id.txt_home_phone_name);
            recycler_home_item = itemView.findViewById(R.id.recycler_home_item);
        }
    }

    // 头部
    private class HeadHolder extends RecyclerView.ViewHolder{
        HeadHolder(View itemView) {
            super(itemView);
        }
    }

    // 尾部
    private class FootHolder extends RecyclerView.ViewHolder{
        public FootHolder(View itemView) {
            super(itemView);
        }
    }

    /*之下的方法都是为了方便操作，并不是必须的*/

    //在指定位置插入，原位置的向后移动一格
    public boolean addItem(int position, PhoneListDataBean bean) {
        if (position < list.size() && position >= 0) {
            list.add(position, bean);
            notifyItemInserted(position);
            return true;
        }
        return false;
    }

    //去除指定位置的子项
    public boolean removeItem(int position) {
        if (position < list.size() && position >= 0) {
            list.remove(position);
            notifyItemRemoved(position);
            return true;
        }
        return false;
    }

    //清空显示数据
    public void clearAll() {
        list.clear();
        notifyDataSetChanged();
    }
}
